﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.NetworkRigidbody
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using UnityEngine;

#nullable disable
namespace Netick.Unity;

/// <summary>
/// This component is used to replicate the position and rotation of an object. Therefore, it’s the same as the Network Transform, the difference is that Network Rigidbody must be used if you want to have client-side prediction over PhysX objects since it replicates several other properties necessary for correct prediction.
/// </summary>
[IgnoreCodeGen]
[AddComponentMenu("Netick/Network Rigidbody")]
[RequireComponent(typeof (Rigidbody))]
[DisallowMultipleComponent]
public sealed class NetworkRigidbody : NetworkTransform, IPhysicsComponent
{
  private Rigidbody _rigid;
  internal int _syncToOnlyInputSource;

  internal unsafe Vector3 NetworkedVelocity
  {
    get => NetickUnityUtils.GetVector3(this.StatePtr + 8, this._posPrecision);
    set => NetickUnityUtils.SetVector3(this.StatePtr + 8, value, this._posInversePrecision);
  }

  internal unsafe Vector3 NetworkedAngularVelocity
  {
    get => NetickUnityUtils.GetVector3(this.StatePtr + 11, this._posPrecision);
    set => NetickUnityUtils.SetVector3(this.StatePtr + 11, value, this._posInversePrecision);
  }

  internal unsafe bool NetworkedIsKinematic
  {
    get => this.StatePtr[14] == 1;
    set
    {
      int num1 = this.StatePtr[14];
      int num2 = value ? 1 : 0;
      this.StatePtr[14] = num2;
      if (!this.IsServer || num1 == num2)
        return;
      this.Entity.MarkAltered(this.StatePtr + 14);
    }
  }

  public override int InternalGetStateSizeWords() => 16 /*0x10*/;

  public override unsafe void InternalReset()
  {
    base.InternalReset();
    this._rigid = this.GetComponent<Rigidbody>();
    this.NetworkedVelocity = this._rigid.velocity;
    this.NetworkedAngularVelocity = this._rigid.angularVelocity;
    this.NetworkedIsKinematic = this._rigid.isKinematic;
    if (!this.Sandbox.IsServer)
      return;
    for (int index = 8; index < 7; ++index)
      this.Entity.MarkAltered(this.StatePtr + index);
  }

  public override void NetworkStart()
  {
    base.NetworkStart();
    this._syncToOnlyInputSource = this.Object.PredictionMode == Relevancy.InputSource ? 1 : 0;
  }

  public override void NetworkFixedUpdate()
  {
    base.NetworkFixedUpdate();
    if (!this.IsClient)
      return;
    if (this.Object.PredictionMode == Relevancy.InputSource)
    {
      if (this.IsInputSource)
      {
        this._rigid.isKinematic = this.NetworkedIsKinematic;
      }
      else
      {
        if (this._rigid.isKinematic)
          return;
        this._rigid.isKinematic = true;
      }
    }
    else
      this._rigid.isKinematic = this.NetworkedIsKinematic;
  }

  public override void NetcodeIntoGameEngine()
  {
    base.NetcodeIntoGameEngine();
    if (this._syncPosition)
      this._rigid.position = this.transform.position;
    if (this._syncRot)
      this._rigid.rotation = this.transform.rotation;
    if (this.IsClient)
    {
      if (this.Object.PredictionMode == Relevancy.InputSource)
      {
        if (this.IsInputSource)
          this._rigid.isKinematic = this.NetworkedIsKinematic;
        else if (!this._rigid.isKinematic && !this.IsInputSource)
          this._rigid.isKinematic = true;
      }
      else
        this._rigid.isKinematic = this.NetworkedIsKinematic;
    }
    if (this._rigid.isKinematic || (double) (this.NetworkedVelocity - this._rigid.velocity).sqrMagnitude < 1.0000000116860974E-07 && (double) (this.NetworkedAngularVelocity - this._rigid.angularVelocity).sqrMagnitude < 1.0000000116860974E-07)
      return;
    this._rigid.velocity = this.NetworkedVelocity;
    this._rigid.angularVelocity = this.NetworkedAngularVelocity;
  }

  public override unsafe void GameEngineIntoNetcode()
  {
    base.GameEngineIntoNetcode();
    this.NetworkedIsKinematic = this._rigid.isKinematic;
    int* numPtr = (int*) &new TransformBuffer();
    NetickUnityUtils.SetVector3(numPtr, this._rigid.velocity, this._posInversePrecision);
    this.Entity.SetState(numPtr, this.StatePtr + 8, 3, 0, this._syncToOnlyInputSource);
    NetickUnityUtils.SetVector3(numPtr, this._rigid.angularVelocity, this._posInversePrecision);
    this.Entity.SetState(numPtr, this.StatePtr + 11, 3, 0, this._syncToOnlyInputSource);
  }
}
